//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class NumericLiteral : NumericDataSource, IOfficeXmlSerializableAs
    {
        public NumericLiteral()
        {
            
        }

        public NumericLiteral(params double[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                _numericValues.Add(new NumericValue(i, values[i]));
            }
        }
        public NumericLiteral(params double?[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                double? v = values[i];
                if (v != null)
                    _numericValues.Add(new NumericValue(i, v.Value));
            }
        }
        private readonly ObservableCollection<NumericValue> _numericValues = new ObservableCollection<NumericValue>();
        private NumericReference _parent;
        private string formatCode = "General";

        public string FormatCode
        {
            get { return formatCode; }
            set { formatCode = value; }
        }

        public NumericReference Parent
        {
            get { return _parent; }
            set { _parent = value; }
        }

        public override IEnumerable<NumericValue> NumericValues
        {
            get { return _numericValues; }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                FormatCode = c.ReadOptionalElementString("formatCode");
                c.ReadOptionalElementString("ptCount");
                while (c.ReaderLocalName == "pt")
                {
                    var v = new NumericValue(Parent);
                    v.ReadXmlCore(c);
                    _numericValues.Add(v);
                }
            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.WriteOptionalString("formatCode", FormatCode);
                c.WriteElementIntegerAttribute("ptCount", _numericValues.Count);
                foreach (NumericValue v in Values)
                    v.WriteXmlCore(c);
            }
        }

        #endregion

        public void AddValue(NumericValue value)
        {
            _numericValues.Add(value);
        }


        protected override void ReadXmlCore(ReadContext context)
        {
            IOfficeXmlSerializableAs t = this;
            t.ReadXmlAs("numLit", context);
        }

        protected override void WriteXmlCore(WriteContext context)
        {
            IOfficeXmlSerializableAs t = this;
            t.WriteXmlAs("numLit", context);
        }
    }
}